package util.bio.annotation;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;

public class NamedScoredStartStop extends StartStop{
	//fields
	private String name;  //or rank
	private float score;
	
	//constructor
	public NamedScoredStartStop (int start, int stop, String name, float score){
		super(start, stop);
		this.name = name;
		this.score = score;
	}

	//methods
	/**Loads a binary file containing int,int,float,String (start,stop,score,name)
	 * @return an UNSORTED array! or null if something bad happened.*/
	public static NamedScoredStartStop[] loadBinary(File binaryFile){
		ArrayList<NamedScoredStartStop> sss = new ArrayList<NamedScoredStartStop>(10000);
		DataInputStream dis = null;
		try {
			dis = new DataInputStream(new BufferedInputStream(new FileInputStream(binaryFile)));
			while (true){
				int start = dis.readInt();
				int stop = dis.readInt();
				float score = dis.readFloat();
				//read name
				byte[] barray = new byte[dis.readInt()];
				dis.readFully(barray);
				String name = new String(barray);
				sss.add(new NamedScoredStartStop(start,stop,name, score));
			}

		} catch (EOFException eof){
			NamedScoredStartStop[] s = new NamedScoredStartStop[sss.size()];
			sss.toArray(s);
			return s;
		}
		catch (Exception e){
			e.printStackTrace();
			return null;
		} finally {
			if (dis != null) {
				try {
					dis.close();
				} catch (IOException ignored) {
				}
			}
		}
	}
	public String toString(){
		return start+"\t"+end+"\t"+name+"\t"+score;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public float getScore() {
		return score;
	}

	public void setScore(float score) {
		this.score = score;
	}
	

}
